﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using iOak.Business.Resumes;
using iOak.Business.Campaigns;
using iOak.Domain.Resumes;
using System.Web.UI.WebControls;
using System.Web.UI;
using iOak.Common.Untity;

namespace iOak.Web.TemplateHelper
{
    public class TemplateCommonViewHelper
    {
        private static readonly ResumeTemplateService itemResumeTemplateService = new ResumeTemplateService();
        private static readonly ResumeFieldService itemResumeFieldService = new ResumeFieldService();
        private static readonly ResumeTemplateFieldsService itemResumeTemplateFieldsService = new ResumeTemplateFieldsService();
        private static readonly ResumeDetailService itemResumeDetailService = new ResumeDetailService();
        private static readonly ResumeService itemResumeService = new ResumeService();
        private static readonly ApplicationService itemApplicationService = new ApplicationService();
        private static readonly ApplicationCandidateService itemApplicationCandidateService = new ApplicationCandidateService();

        /// <summary>
        /// 获取模板元数据对象
        /// </summary>
        /// <param name="templateId">模板ID</param>
        /// <returns>元数据对象</returns>
        public static List<ResumeField> GetTemplateFileList(Guid templateId)
        {
            // 获取默认控件
            var listDefault = itemResumeFieldService.GetDefaultFieldList();

            // 获取模板控件
            var templateList = itemResumeTemplateFieldsService.GetListByTemplateId(templateId);

            // 转换对象
            var changeList = itemResumeTemplateFieldsService.ChangeListToField(templateList);
            itemResumeFieldService.AddDefaultList(changeList, listDefault);

            return changeList;
        }

        /// <summary>
        /// 控件初始化
        /// </summary>
        /// <param name="resumeFieldList">元数据列表</param>
        /// <param name="templateControl">当前模板</param>
        /// <param name="resumeDetailList">值列表</param>
        public static void InitializationTemplate(List<ResumeField> resumeFieldList, Control templateControl,
            List<ResumeDetail> resumeDetailList)
        {
            resumeFieldList.ForEach(m =>
            {
                var lableControl = templateControl.FindControl("lbl" + m.FieldName) as Label;
                var valueItem = resumeDetailList.FirstOrDefault(v => v.Key == m.FieldName);
                string labelText = m.DisplayName;
                if (string.IsNullOrWhiteSpace(labelText))
                {
                    labelText = m.LabelText;
                }
                if (lableControl != null)
                {
                    lableControl.Text = labelText;
                }
                if (valueItem != null)
                {
                    var labelTxt = templateControl.FindControl("lblTxt" + m.FieldName) as Label;
                    var labelDdl = templateControl.FindControl("lblDdl" + m.FieldName) as Label;
                    var labelChk = templateControl.FindControl("lblChk" + m.FieldName) as Label;
                    if (labelTxt != null)
                    {
                        labelTxt.Text = valueItem.ValueString;
                    }
                    else if (labelDdl != null)
                    {
                        labelDdl.Text = valueItem.ValueString;
                    }
                    else if (labelChk != null)
                    {
                        if (valueItem.ValueString.ToLower().Equals("true"))
                        {
                            labelChk.Text = "是";
                        }
                        else if (valueItem.ValueString.ToLower().Equals("false"))
                        {
                            labelChk.Text = "否";
                        }
                    }
                }
            });
        }
    }
}